package com.amigo.online.provider.video.common;


import com.amigo.online.common.base.security.LocalUser;
import com.amigo.online.common.core.response.StatusCode;
import com.amigo.online.common.param.result.LocalUserInfo;
import com.amigo.online.common.param.result.UserInfo;
import com.amigo.online.provider.video.exception.VideoException;


public class GlobalController {
    public static UserInfo getNowUser() {
        LocalUserInfo info = LocalUser.getUser();
        if (info == null) {
            throw new VideoException(StatusCode.USER_EXCEPTION);
        }
        UserInfo u = info.getUser();
        if (u.getId() == null || u.getId().intValue() <= 0) {
            throw new VideoException(StatusCode.USER_EXCEPTION);
        }
        return u;
    }

    public static Integer checkPage(Integer page, Integer size) {
        if (page == null || size == null || size.intValue() <= 0 || size.intValue() > 50) {
            throw new VideoException(StatusCode.BAD_REQUEST);
        }
        if (page.intValue() <= 1) {
            page = 1;
        }
        return page;
    }

    public static Long getUserIdIfExists() {
        LocalUserInfo info = LocalUser.getUser();
        if (info == null) {
            return null;
        }
        UserInfo userInfo = info.getUser();
        if (userInfo.getId() == null || userInfo.getId().intValue() <= 0) {
            return null;
        } else {
            return userInfo.getId();
        }
    }
}
